'''
Created on 2010-3-3

@author: Administrator
'''

import vtk
import os

from jolly.datastructures.GeneralTree import GeneralTree
from jolly.datamanagement.vtkPythonMetaDataSetSequence import *
from jolly.datamanagement.vtkPythonMetaDataSet import *
from jolly.datamanagement.vtkPythonMetaVolumeMesh import *
from jolly.datamanagement.vtkPythonMetaSurfaceMesh import *
from jolly.datamanagement.vtkPythonMetaImageData import *


class vtkPythonDataTreeManager(vtk.vtkObject):
    '''
    vtkPythonDataTreeManager
    @summary: vtkPythonDataManager use linear structure to manage the vtkdataobject,
    but from tree organization this vtkdataobject is more convenient.Because a Vomule
    Imagedata should only has a orgin dataset, and many mask dataset and surface.
    mask imagedataset and surfacedataset is one-to-one correspondence 
    we use the vtkPythonMetaDataSet as trees' key  
    '''
    
    def __init__(self):
        '''
        Constructor
        '''
        super(vtkPythonDataTreeManager, self).__init__()
        self.__MetaDataSetList=GeneralTree("Root")  # fake node
    
    def __str__(self):
        '''
        '''
        msg = "has %d MetaDataSets... "%(self.__MetaDataSetList.count-1)
        for i in xrange(self.__MetaDataSetList.getDegree()):
            for metadataset in self.__MetaDataSetList.getSubtree(i):
                msg+=str(metadataset)
        return msg
    
    def GetOriginDataSet(self, id):
        '''
        Primary method to get a OriginDataSet of the manager from its index.
        OriginDataSet is at the level-1 of tree
        '''
        if id<0 or id>self.__MetaDataSetList.getDegree():
            return None
        return self.__MetaDataSetList.getSubtree(i).getKey()
    
    def GetMetaDataSet(self, subtree, id):
        '''
        Primary method to get a maskdataset of the manager from its index.
        level-2 : MetaImageData
        level-3 : MetaSurfaceData
        level-4 : MetaVolumeData
        '''
        assert isinstance(subtree, GeneralTree)
        if subtree is None or id<0 or id>self.subtree.getDegree():
            return None
        return self.subtree.getSubtree(i).getKey()
    
    def IsInManager(self, metadataset):
        '''
         returns true if the given metadataset is referenced in the manager.
         if yes, returns the its index in the list.
         @param metadataset: vtkMetaDataSet
         @rtype: int
        '''
        for i in xrange(self.__MetaDataSetList.getDegree()):
            for dataset in self.__MetaDataSetList.getSubtree(i):
                if dataset is metadataset:
                    return True
        return False
    
    def IsNameInManager(self, name):
        '''
        returns true if the given name is used in the manager.
        if yes, returns the its index in the list.
        @param name: str
        @rtype: in
        '''
        for i in xrange(self.__MetaDataSetList.getDegree()):
            for dataset in self.__MetaDataSetList.getSubtree(i):
    
    def GetMetaDataSetFromName(self, name):
        '''
        Secondary method to get a metadataset of the manager
         from the metadataset's name.
         returns NULL object if the name is not referenced in datamanager.
         @param name: str 
         @rtype: vtkMetaDataSet
        '''
        id=self.IsNameInManager(name)
        if id<0 or id>len(self.__MetaDataSetList):
            return None
        else:
            return self.__MetaDataSetList[id]
       
        